package com.henanuniversity.dao.impl;

import com.henanuniversity.dao.FoodDao;
import com.henanuniversity.po.Food;
import com.henanuniversity.utils.DBUtils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class FoodDaoImpl implements FoodDao {
    Connection conn=null;
    PreparedStatement pst=null;
    ResultSet rs=null;


    @Override
    public List<Food> listFoodByBusinessId(Integer businessId) {
        List<Food> list=new ArrayList<>();
        String sql="select * from food where businessId=? order by foodId";
        try {
            conn = DBUtils.getConnection();
            pst= conn.prepareStatement(sql);
            pst.setInt(1,businessId);
            rs=pst.executeQuery();
            while(rs.next()){
                Food food=new Food();
                food.setFoodId(rs.getInt("foodId"));
                food.setFoodName(rs.getString("foodName"));
                food.setFoodExplain(rs.getString("foodExplain"));
                food.setFoodImg(rs.getString("foodImg"));
                food.setBusinessId(rs.getInt("businessId"));
                food.setRemarks(rs.getString("remarks"));
                food.setFoodPrice(rs.getDouble("foodPrice"));
                list.add(food);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
             DBUtils.close(rs,pst);
        }
        return list;
    }
}
